*Generates TABLE 7: Market Shares of All Antidepressant Prescriptions, 2006 and 2014
*Version 15 Stata

set more off

*Establish working directories
local d3 Geographic Data
local d5 Antidepressant Data
local d22 Entropy Data

*Bring in data (basically raw antidepressant data)
cd `d5'
use year product tot_scripts if year==2006 | year==2014 using provider_product_cat_allyears.dta, clear

*******************************************************************************
*SAMPLE SELECTION
*Kick out low prescribers: need to have prescribed at least 12 scripts in one year
*******************************************************************************
collapse (sum) tot_scripts, by(year product)

*Recode product definitions

*DEFINITION TWO - Product, ignoring salts / dosage mechanism
generate product2 = product
replace product2 = "Budeprion" if product=="BUDEPRION SR" | product=="BUDEPRION XL"
replace product2 = "Bupropion" if product=="BUPROPION HCL" | product=="BUPROPION HCL SR" | product=="BUPROPION HCL SR W" | product=="BUPROPION HCL XL"
replace product2 = "Desvenlafaxine" if product=="DESVENLAFAXINE ER" | product=="DESVENLAFAXINE FUM ER"
replace product2 = "Effexor" if product=="EFFEXOR" | product=="EFFEXOR XR"
replace product2 = "Fluvoxamine" if product=="FLUVOXAMINE MAL" | product=="FLUVOXAMINE MAL ER"
replace product2 = "Imipramine" if product=="IMIPRAMINE HCL" | product=="IMIPRAMINE PAM"
replace product2 = "Limbitrol" if product=="LIMBITROL" | product=="LIMBITROL DS"
replace product2 = "Luvox" if product=="LUVOX" | product=="LUVOX CR"
replace product2 = "Paroxetine" if product=="PAROXETINE CR" | product=="PAROXETINE HCL" | product=="PAROXETINE HCL ER"
replace product2 = "Paxil" if product=="PAXIL" | product=="PAXIL CR"
replace product2 = "Prozac" if product=="PROZAC" | product=="PROZAC WEEKLY"
replace product2 = "Remeron" if product=="REMERON" | product=="REMERON SOLTAB"
replace product2 = "Tofranil" if product=="TOFRANIL" | product=="TOFRANIL-PM"
replace product2 = "Venlafaxine" if product=="VENLAFAXINE HCL" | product=="VENLAFAXINE HCL ER"
replace product2 = "Wellbutrin" if product=="WELLBUTRIN" | product=="WELLBUTRIN SR" | product=="WELLBUTRIN XL"
replace product2= "Chlorpromazine" if product=="CHLORPROMAZINE HCL"
replace product2= "Olanz/Fluox" if product=="OLANZAP/FLUOX HCL"
replace product2= "Quetiapine" if product=="QUETIAPINE FUM"
replace product2= "Thioridazine" if product=="THIORIDAZINE HCL"
replace product2= "Trifluoperazin" if product=="TRIFLUOPERAZIN HCL"
replace product2= "Ziprasidone" if product=="ZIPRASIDONE HCL"

*DEFINITION THREE - Product molecule, as defined by active ingredient
*NOTE: As of January 2018, fix the product3 definition for three products: Bupropion, CLOMIPRAMINE HCL, and ADAPIN.
generate product3 = product2
replace product3 = "DOXEPIN HCL" if product2=="ADAPIN"
replace product3 = "Bupropion" if product2=="Budeprion"
replace product3 = "CLOMIPRAMINE HCL" if product3=="CLOMIPRAMINE"
replace product3 = "CLOMIPRAMINE HCL" if product2=="ANAFRANIL"
replace product3 = "Bupropion" if product2=="APLENZIN" | product2=="FORFIVO XL" | product2=="Wellbutrin"
replace product3 = "AMOXAPINE" if product2=="ASENDIN"
replace product3 = "CITALOPRAM HBR" if product2=="CELEXA"
replace product3 = "DULOXETINE HCL" if product2=="CYMBALTA"
replace product3 = "TRAZODONE HCL" if product2=="DESYREL" | product2=="OLEPTRO"
replace product3 = "Venlafaxine" if product2=="Effexor"
replace product3 = "AMITRIPTYLINE HCL" if product2=="ELAVIL" | product2=="Limbitrol"
replace product3 = "PERPHENAZN/AMITRIP" if product2=="ETRAFON" | product2=="TRIAVIL"
replace product3 = "Desvenlafaxine" if product2=="KHEDEZLA ER" | product2=="PRISTIQ"
replace product3 = "ESCITALOPRAM OXAL" if product2=="LEXAPRO"
replace product3 = "MAPROTILINE HCL" if product2=="LUDIOMIL"
replace product3 = "Fluvoxamine" if product2=="Luvox"
replace product3 = "PHENELZINE SULF" if product2=="NARDIL"
replace product3 = "DESIPRAMINE HCL" if product2=="NORPRAMIN"
replace product3 = "NORTRIPTYLINE HCL" if product2=="PAMELOR"
replace product3 = "TRANYLCYPRO SULF" if product2=="PARNATE"
replace product3 = "Paroxetine" if product2=="Paxil" | product2=="PEXEVA"
replace product3 = "FLUOXETINE HCL" if product2=="Prozac" | product2=="SENTROXATINE" 
replace product3 = "MIRTAZAPINE" if product2=="Remeron"
replace product3 = "NEFAZODONE HCL" if product2=="SERZONE"
replace product3 = "DOXEPIN HCL" if product2=="SINEQUAN"
replace product3 = "TRIMIPRAMINE MAL" if product2=="SURMONTIL"
replace product3 = "Imipramine" if product2=="Tofranil"
replace product3 = "TRAZODONE HCL" if product2=="TRAZAMINE"
replace product3 = "PROTRIPTYLINE HCL" if product2=="VIVACTIL"
replace product3 = "SERTRALINE HCL" if product2=="ZOLOFT"

*DEFINITION FOUR - Product class
*NOTE: SSRI, SNRI, NDRI, SARI, with tricyclics and tetracyclics together
*	Brintellix as SSRI.
*	Nefazodone as SARI given same action as Trazodone.
generate product4 = ""
replace product4 = "SSRI" if product3=="CITALOPRAM HBR" | product3=="ESCITALOPRAM OXAL" | product3=="FLUOXETINE HCL" | product3=="Fluvoxamine" | product3=="Paroxetine" | product3=="SERTRALINE HCL" | product3=="VIIBRYD" | product3=="BRINTELLIX"
replace product4 = "SNRI" if product3=="DULOXETINE HCL" | product3=="Desvenlafaxine" | product3=="FETZIMA" | product3=="Venlafaxine"
replace product4 = "NDRI" if product3=="Bupropion"
replace product4 = "SARI" if product3=="TRAZODONE HCL" | product3=="NEFAZODONE HCL"
replace product4 = "MAOI" if product3=="EMSAM" | product3=="MARPLAN" | product3=="PHENELZINE SULF" | product3=="TRANYLCYPRO SULF"
replace product4 = "TCA" if product4==""

*DEFINITION FIVE - "Big" sellers and others
*NOTE: There are 11 big sellers by molecule; other molecules are grouped into 
*	an "other" category.
generate product5 = product3
replace product5 = "Other" if product3!="SERTRALINE HCL" & product3!="CITALOPRAM HBR" & product3!="FLUOXETINE HCL" & product3!="ESCITALOPRAM OXAL" & product3!="Paroxetine" & product3!="Venlafaxine" & product3!="DULOXETINE HCL" & product3!="Bupropion" & product3!="TRAZODONE HCL" & product3!="AMITRIPTYLINE HCL" & product3!="MIRTAZAPINE"

*Label variables
label variable product "Product: Raw IMS product"
label variable product2 "Product: Without salt / mechanism"
label variable product3 "Product: Active ingredient"
label variable product4 "Product: Class"
label variable product5 "Product: Top sellers"

*Calculate total prescriptions written per year (including all products)
by year, s : egen double total=sum(tot_scripts)

*******************************************************************************
*Columns 1 and 2: Drug class
*******************************************************************************
replace product4 = "TeCA" if product3=="MAPROTILINE HCL" | product3=="MIRTAZAPINE" | product3=="AMOXAPINE"

*Get rid of MAOIs
drop if product4=="MAOI"

preserve

*Collapse into drug class records
collapse (sum) tot_scripts (first) total, by(product4 year)

*Calculate share by drug class
generate share = tot_scripts/total

*Reshape wide
drop tot_scripts total
reshape wide share, i(product4) j(year)

*Generate order
generate order = 1 if product4=="SSRI"
replace order=2 if product4=="SNRI"
replace order=3 if product4=="NDRI"
replace order=4 if product4=="SARI"
replace order=5 if product4=="TCA"
replace order=6 if product4=="TeCA"
sort order

list product4 share*

restore

*Hard code alternate branded products into "branded" bin; collapse into product
replace product2 = "Wellbutrin" if product2=="Budeprion" | product2=="FORFIVO XL" | product2=="APLENZIN"
replace product2 = "ELAVIL" if product2=="Limbitrol"
replace product2 = "TRAZODONE HCL" if product2=="OLEPTRO" | product2=="DESYREL"
replace product2 = "Paxil" if product2=="PEXEVA"

*Hard code "Other" drugs into other categories within class
replace product2 = "Other" + " " + product4 if product5=="Other"
replace product3 = "Other" + " " + product4 if product5=="Other"

*Collapse into drug-class records
collapse (sum) tot_scripts (first) total, by(year product2 product3 product4 product5 year)

*Parse variables
generate share = tot_scripts/total
drop tot_scripts total

*Reshape the data wide
reshape wide share, i(product2 product3 product4) j(year)
replace share2006=0 if share2006==.

*Generate order
generate order = 1 if product2=="SERTRALINE HCL"
replace order = 2 if product2=="ZOLOFT"
replace order = 3 if product2=="CITALOPRAM HBR"
replace order = 4 if product2=="CELEXA"
replace order = 5 if product2=="FLUOXETINE HCL"
replace order = 6 if product2=="Prozac"
replace order = 7 if product2=="ESCITALOPRAM OXAL"
replace order = 8 if product2=="LEXAPRO"
replace order = 9 if product2=="Paroxetine"
replace order = 10 if product2=="Paxil"
replace order = 11 if product2=="Venlafaxine"
replace order = 12 if product2=="Effexor"
replace order = 13 if product2=="DULOXETINE HCL"
replace order = 14 if product2=="CYMBALTA"
replace order = 15 if product2=="Bupropion"
replace order = 16 if product2=="Wellbutrin"
replace order = 17 if product2=="TRAZODONE HCL"
replace order = 18 if product2=="AMITRIPTYLINE HCL"
replace order = 19 if product2=="ELAVIL"
replace order = 20 if product2=="MIRTAZAPINE"
replace order = 21 if product2=="Remeron"
sort order

*******************************************************************************
*Columns 5 and 6: Specific product
*******************************************************************************
list product2 share*

preserve


*Collapse data into molecule
collapse (sum) share* (min) order, by(product3 product4)

*******************************************************************************
*Columns 3 and 4: Molecule
*******************************************************************************
sort order
list product3 share*



